package com.caigou.service.impl;

import com.caigou.bean.cms.entity.ChannelData;
import com.caigou.component.*;
import com.caigou.enums.ResponseEnum;
import com.caigou.service.ArticleService;
import com.caigou.service.ChannelDataService;
import com.caigou.service.GovProListService;
import com.caigou.service.PageService;
import com.caigou.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("pageService")
public class PageServiceImpl implements PageService {
    @Resource
    private TemplateComponent templateComponent;
    @Resource
    private HtmlBuilderComponent htmlBuilderComponent;
    @Resource
    private HtmlHomePageBuilderComponent htmlHomePageBuilderComponent;
    @Resource
    private HtmlListPageBuilderComponent htmlListPageBuilderComponent;
    @Resource
    private HtmlDetailBuilderComponent htmlDetailBuilderComponent;
    @Resource
    private HtmlListComponent htmlListComponent;

    @Resource
    private GovProListService govProListService;
    @Resource
    private ArticleService articleService;
    @Resource
    private ChannelDataService channelDataService;

    @Value("${domain}")
    private String domain;
    @Value("${domainJunDui}")
    private String domainJunDui;
    @Value("${domainQiChe}")
    private String domainQiChe;
    @Value("${domainIT}")
    private String domainIT;
    @Value("${domainKongTiao}")
    private String domainKongTiao;
    @Value("${domainDianTi}")
    private String domainDianTi;
    @Value("${domainJiaJu}")
    private String domainJiaJu;

    @Value("${domainHuBei}")
    private String domainHuBei;
    @Value("${domainHeNan}")
    private String domainHeNan;
    @Value("${domainLiaoNing}")
    private String domainLiaoNing;
    @Value("${domainFuJian}")
    private String domainFuJian;
    @Value("${domainHaiNan}")
    private String domainHaiNan;
    @Value("${domainHeBei}")
    private String domainHeBei;// 河北
    @Value("${domainShanDong}")
    private String domainShanDong;// 山东
    @Value("${domainJiangSu}")
    private String domainJiangSu;// 江苏
    @Value("${domainShaanXi}")
    private String domainShaanXi;// 陕西
    @Value("${domainBeiJing}")
    private String domainBeiJing;// 北京
    @Value("${domainZheJiang}")
    private String domainZheJiang;// 浙江
    @Value("${domainYunNan}")
    private String domainYunNan;// 云南
    @Value("${domainGuiZhou}")
    private String domainGuiZhou;// 贵州
    @Value("${domainChongQing}")
    private String domainChongQing;// 重庆
    @Value("${domainTianJin}")
    private String domainTianJin;// 天津
    @Value("${domainShenZhen}")
    private String domainShenZhen;// 深圳
    @Value("${domainQingDao}")
    private String domainQingDao;// 青岛
    @Value("${domainDaLian}")
    private String domainDaLian;// 大连
    @Value("${domainXiZang}")
    private String domainXiZang;// 西藏
    @Value("${domainXinJiang}")
    private String domainXinJiang;// 新疆
    @Value("${domainNingXia}")
    private String domainNingXia;// 宁夏
    @Value("${domainGuangXi}")
    private String domainGuangXi;// 广西
    @Value("${domainNeiMengGu}")
    private String domainNeiMengGu;// 内蒙古
    @Value("${domainGanSu}")
    private String domainGanSu;// 甘肃
    @Value("${domainBingTuan}")
    private String domainBingTuan;// 新疆生产建设兵团

    @Override
    public Result buildFragment() {
        Map<String,Object> map = new HashMap<>();
        map.put("domain",domain);
        map.put("domainJunDui",domainJunDui);
        map.put("domainQiChe",domainQiChe);
        map.put("domainIT",domainIT);
        map.put("domainKongTiao",domainKongTiao);
        map.put("domainDianTi",domainDianTi);
        map.put("domainJiaJu",domainJiaJu);

        map.put("domainHuBei", domainHuBei);
        map.put("domainHeNan", domainHeNan);
        map.put("domainLiaoNing", domainLiaoNing);
        map.put("domainFuJian", domainFuJian);
        map.put("domainHaiNan", domainHaiNan);
        map.put("domainHeBei", domainHeBei);
        map.put("domainShanDong", domainShanDong);
        map.put("domainJiangSu", domainJiangSu);
        map.put("domainShaanXi", domainShaanXi);
        map.put("domainBeiJing", domainBeiJing);
        map.put("domainZheJiang", domainZheJiang);
        map.put("domainYunNan", domainYunNan);
        map.put("domainGuiZhou", domainGuiZhou);
        map.put("domainChongQing", domainChongQing);
        map.put("domainTianJin", domainTianJin);
        map.put("domainShenZhen", domainShenZhen);
        map.put("domainQingDao", domainQingDao);
        map.put("domainDaLian", domainDaLian);
        map.put("domainXiZang", domainXiZang);
        map.put("domainXinJiang", domainXinJiang);
        map.put("domainNingXia", domainNingXia);
        map.put("domainGuangXi", domainGuangXi);
        map.put("domainNeiMengGu", domainNeiMengGu);
        map.put("domainGanSu", domainGanSu);
        map.put("domainBingTuan", domainBingTuan);
        try {
            //更新所有频道的header碎片
            htmlBuilderComponent.buildHeaderHtml("101");// 更新首页频道头
            htmlBuilderComponent.buildHeaderHtml("102");// 更新采购人频道头
            htmlBuilderComponent.buildHeaderHtml("103");// 更新采购实务频道头
            htmlBuilderComponent.buildHeaderHtml("104");// 更新监管频道头
            htmlBuilderComponent.buildHeaderHtml("105");// 更新研究院频道头
            htmlBuilderComponent.buildHeaderHtml("106");// 更新军队频道头
            htmlBuilderComponent.buildHeaderHtml("107");// 更新汽车频道头
            htmlBuilderComponent.buildHeaderHtml("108");// 更新IT频道头
            htmlBuilderComponent.buildHeaderHtml("109");// 更新电梯频道头
            htmlBuilderComponent.buildHeaderHtml("110");// 更新空调电器频道头
            htmlBuilderComponent.buildHeaderHtml("111");// 更新家具频道头

            //更新分站的header碎片
            htmlBuilderComponent.buildHeaderHtml("201");//河北
            htmlBuilderComponent.buildHeaderHtml("301");//河南
            htmlBuilderComponent.buildHeaderHtml("401");//山东
            htmlBuilderComponent.buildHeaderHtml("601");//辽宁
            htmlBuilderComponent.buildHeaderHtml("901");//江苏
            htmlBuilderComponent.buildHeaderHtml("1001");//浙江
            htmlBuilderComponent.buildHeaderHtml("1201");//福建
            htmlBuilderComponent.buildHeaderHtml("1401");//湖北
            htmlBuilderComponent.buildHeaderHtml("1701");//海南
            htmlBuilderComponent.buildHeaderHtml("1901");//贵州
            htmlBuilderComponent.buildHeaderHtml("2001");//云南
            htmlBuilderComponent.buildHeaderHtml("2101");//陕西
            htmlBuilderComponent.buildHeaderHtml("2401");//北京
            htmlBuilderComponent.buildHeaderHtml("2501");//天津
            htmlBuilderComponent.buildHeaderHtml("2601");//重庆
            htmlBuilderComponent.buildHeaderHtml("3801");//深圳
            htmlBuilderComponent.buildHeaderHtml("3501");//青岛
            htmlBuilderComponent.buildHeaderHtml("3401");//大连
            htmlBuilderComponent.buildHeaderHtml("3201");//西藏
            htmlBuilderComponent.buildHeaderHtml("3101");//新疆
            htmlBuilderComponent.buildHeaderHtml("3001");//宁夏
            htmlBuilderComponent.buildHeaderHtml("2901");//广西
            htmlBuilderComponent.buildHeaderHtml("2801");//内蒙古
            htmlBuilderComponent.buildHeaderHtml("2201");//甘肃
            htmlBuilderComponent.buildHeaderHtml("3301");//甘肃

            //更新所有频道的footer碎片
            htmlBuilderComponent.buildFooterHtml();
            htmlBuilderComponent.buildGovFooterHtml();

            // 网站首页
            templateComponent.createHtml("HOMEFRAGMENTLEFTMENU",map);// 左侧菜单
            // 采购人
            templateComponent.createHtml("PURCHASERFRAGMENTLEFTMENU",map);// 左侧菜单
            // 采购实务
            templateComponent.createHtml("PRACTICEFRAGMENTLEFTMENU",map);// 左侧菜单
            // 监管
            templateComponent.createHtml("SUPERVISEFRAGMENTLEFTMENU",map);// 左侧菜单
            // 研究院
            templateComponent.createHtml("INSTITUTEFRAGMENTLEFTNAV",map);// 左侧菜单
            // 军队
            templateComponent.createHtml("ARMYFRAGMENTLEFTMENU",map);// 左侧菜单
            //列表页碎片生成
            htmlBuilderComponent.buildItemListHtml();
            //频道推送修改--更新静态页(碎片)数据 更新首页右侧亚利聊政采
            htmlHomePageBuilderComponent.buildHomePageHtml("100010201");
            // 右侧热点专题
            htmlHomePageBuilderComponent.buildHomePageHtml("101020201");
            // 右侧有问有答
            htmlHomePageBuilderComponent.buildHomePageHtml("101020401");
            // 精品好书
            htmlHomePageBuilderComponent.buildHomePageHtml("100010101");

            //网站首页处理 要闻、采购人、实务、监管、军队、研究院
            htmlHomePageBuilderComponent.buildHomePageHtml("101010101");

            // 首页易采通精品课
            htmlHomePageBuilderComponent.buildHomePageHtml("101010201");
            // 列表数据
            htmlHomePageBuilderComponent.buildHomePageHtml("101010202");
            // 图标数据
            htmlHomePageBuilderComponent.buildHomePageHtml("101010301");

            // 首页下汽车频道
            htmlHomePageBuilderComponent.buildHomePageHtml("101010401");
            // 首页下IT
            htmlHomePageBuilderComponent.buildHomePageHtml("101010402");
            // 首页下家具
            htmlHomePageBuilderComponent.buildHomePageHtml("101010403");
            // 首页下空调电器
            htmlHomePageBuilderComponent.buildHomePageHtml("101010404");
            // 首页下电梯
            htmlHomePageBuilderComponent.buildHomePageHtml("101010405");

            // 汽车频道
            htmlHomePageBuilderComponent.buildHomePageHtml("107010101");
            // IT频道
            htmlHomePageBuilderComponent.buildHomePageHtml("108010101");
            // 家具频道
            htmlHomePageBuilderComponent.buildHomePageHtml("111010101");
            // 空调频道
            htmlHomePageBuilderComponent.buildHomePageHtml("110010101");
            // 电梯频道
            htmlHomePageBuilderComponent.buildHomePageHtml("109010101");

            // 政策解读
            htmlHomePageBuilderComponent.buildHomePageHtml("106010101");


            // 采购人下采购人
            htmlHomePageBuilderComponent.buildHomePageHtml("102010101");
            // 采购人下高校审核
            htmlHomePageBuilderComponent.buildHomePageHtml("102010201");
            // 列表展示
            htmlHomePageBuilderComponent.buildHomePageHtml("102010202");

            // 监管频道
            // 政策发布
            htmlHomePageBuilderComponent.buildHomePageHtml("104010101");
            // 人物观点
            htmlHomePageBuilderComponent.buildHomePageHtml("104010102");
            // 新规解读
            htmlHomePageBuilderComponent.buildHomePageHtml("104010201");
            // 推荐专题
            htmlHomePageBuilderComponent.buildHomePageHtml("104010202");

            //研究院频道处理
            // 政策发布
            htmlHomePageBuilderComponent.buildHomePageHtml("105010101");
            // 人物观点
            htmlHomePageBuilderComponent.buildHomePageHtml("105010102");
            // 新规解读
            htmlHomePageBuilderComponent.buildHomePageHtml("105010201");
            // 推荐专题
            htmlHomePageBuilderComponent.buildHomePageHtml("105010202");

            // 军队频道处理
            htmlHomePageBuilderComponent.buildHomePageHtml("106010101");
            //
            // 采购实务频道处理
            htmlHomePageBuilderComponent.buildHomePageHtml("103010101");
            // 实务热搜
            htmlHomePageBuilderComponent.buildHomePageHtml("103010202");
            // 精品项目
            htmlHomePageBuilderComponent.buildHomePageHtml("103010102");
            // 亚利说采购
            htmlHomePageBuilderComponent.buildHomePageHtml("103010201");
        } catch (Exception e){
            log.error(e.getMessage(), e);
        }
        return Result.success(ResponseEnum.SUCCESS);
    }

    /**
     * 生成所有全局的html静态页面，目前是生成政采榜首页、作者更多文章列表页
     * @return
     */
    @Override
    public Result buildHtml(){
        //生成政采榜首页html页面
        this.buildGovProListHtml();
        //生成作者更多文章列表html页面
        this.buildAuthorPostListHtml();

        //生成个人中心碎片aside.htm,我的收藏、我的评论、我的消息页面会用到此碎片
        this.buildPersonalAsideHtml();
        //生成个人中心-我的收藏页面
        this.buildMyCollectionHtml();
        //生成个人中心-我的评论页面
        this.buildMyCommentHtml();
        //生成个人中心-消息中心页面
        this.buildMessageCenterHtml();
        //生成个人中心-消息中心-消息详情页面
        this.buildMessageDetailHtml();
        //生成个人中心-账号设置页面
        this.buildAccountOptionsHtml();
        //生成个人中心-我要发布页面
        this.buildPublishHtml();
        //生成个人中心-我要发布列表页面
        this.buildPublishListHtml();
        //生成个人中心-我要发布详情页面
        this.buildPublishDetailHtml();

        //生成我要讲课页面
        this.buildGiveLessonHtml();

        //生成底层页-RSS订阅
        this.buildBottomRSSHtml();
        //生成底层页-Tag标签
        this.buildBottomTagHtml();
        //生成专题-图片专题列表
        this.buildAtlasSpecialListHtml();
        //生成专题-音频专题列表
        this.buildAudioSpecialListHtml();
        // 生成专题-资讯专题列表
        this.buildNewsSpecialListHtml();

        //生成搜索结果页面
        this.buildSearchResultListHtml();

        //生成商务合作页面
        this.buildCooperationHtml();
        //生成寻求报道底层页面
        this.buildReportHtml();
        //生成网站地图底层页面
        this.buildWebsiteMapHtml();

        //生成正在建设中页面
        this.buildBuildingHtml();
        //生成登录页面
        this.buildLoginHtml();
        //生成404页面
        this.build404Html();

        //生成tag-list页面
        this.buildTagListHtml();
        //生成header-list页面
        this.buildHeaderListHtml();

        //生成采购人purchaser-list页面
        this.buildPurchaserListHtml();
        //生成实务practice-list页面
        this.buildPracticeListHtml();
        //生成监管supervisor-list页面
        this.buildSupervisorListHtml();
        //生成军队army-list页面
        this.buildArmyListHtml();
        //生成研究院institute-list页面
        this.buildInstituteListHtml();


        //生成汽车automobile-list页面
        this.buildAutomobileListHtml();
        //生成it-list页面
        this.buildItListHtml();
        //生成电梯elevator-list页面
        this.buildElevatorListHtml();
        //生成空调电器conditioner-list页面
        this.buildConditionerListHtml();
        //生成家具furniture-list页面
        this.buildFurnitureListHtml();

        //生成湖北分站hubei-list页面
        this.buildHubeiListHtml();

        return Result.success("生成静态html页面成功");
    }


    @Override
    public Result buildHtmlByArticleType(Integer articleType) {
        List<String> articleIdList = articleService.loadArticleListByType(articleType);
        articleIdList.forEach(item-> {
            // 获取当前文章除RSS频道以外发布的最早的记录--如果没有记录则更新RSS频道，如果有则更新最早的数据
            ChannelData channelData = channelDataService.loadLastChannelData(item);
            if (null == channelData) {
                htmlDetailBuilderComponent.buildArticleHtml(item, "999");
            } else {
                htmlDetailBuilderComponent.buildArticleHtml(item, channelData.getPlateId());
            }
        });
        return null;
    }



    private String buildWebsiteMapHtml() {
        return htmlHomePageBuilderComponent.buildWebsiteMapHtml();
    }

    private String buildReportHtml() {
        return htmlHomePageBuilderComponent.buildReportHtml();
    }


    private String buildCooperationHtml() {
        return htmlHomePageBuilderComponent.buildCooperationHtml();
    }

    private String buildSearchResultListHtml() {
        return htmlHomePageBuilderComponent.buildSearchResultListHtml();
    }

    private String buildAudioSpecialListHtml() {
        return htmlHomePageBuilderComponent.buildAudioSpecialListHtml();
    }

    private String buildNewsSpecialListHtml() {
        return  htmlHomePageBuilderComponent.buildNewsSpecialListHtml();
    }

    private String buildAtlasSpecialListHtml() {
        return htmlHomePageBuilderComponent.buildAtlasSpecialListHtml();
    }

    private String buildBottomTagHtml() {
        return htmlHomePageBuilderComponent.buildBottomTagHtml();
    }

    private String buildBottomRSSHtml() {
        return htmlHomePageBuilderComponent.buildBottomRSSHtml();
    }

    private String buildGovProListHtml(){
        return govProListService.buildHomePage();
    }

    public String buildAuthorPostListHtml(){
        return htmlListPageBuilderComponent.buildAuthorPostListPageHtml();
    }

    private String buildMyCollectionHtml(){
        return htmlHomePageBuilderComponent.buildMyCollectionHtml();
    }

    private String buildPersonalAsideHtml(){
        return htmlBuilderComponent.buildPersonalAsideHtml();
    }

    private String buildMyCommentHtml(){
        return htmlHomePageBuilderComponent.buildMyCommentHtml();
    }

    private String buildMessageCenterHtml(){
        return htmlHomePageBuilderComponent.buildMessageCenterHtml();
    }

    private String buildMessageDetailHtml(){
        return htmlHomePageBuilderComponent.buildMessageDetailHtml();
    }

    private String buildAccountOptionsHtml(){
        return htmlHomePageBuilderComponent.buildAccountOptionsHtml();
    }

    private String buildBuildingHtml(){
        return htmlHomePageBuilderComponent.buildBuildingHtml();
    }
    private String buildLoginHtml(){
        return htmlHomePageBuilderComponent.buildLoginHtml();
    }
    private String build404Html(){
        return htmlHomePageBuilderComponent.build404Html();
    }
    private String buildPublishHtml(){
        return htmlHomePageBuilderComponent.buildPublishHtml();
    }
    private String buildPublishListHtml(){
        return  htmlListComponent.buildPublishListHtml();
    }
    private String buildPublishDetailHtml(){
        return htmlDetailBuilderComponent.buildPublishDetailHtml();
    }

    private String buildGiveLessonHtml(){
        return htmlHomePageBuilderComponent.buildGiveLessonHtml();
    }

    private String buildTagListHtml(){
        return htmlListPageBuilderComponent.buildListPageConfigHtml("100");
    }
    private String buildHeaderListHtml(){
        return htmlListPageBuilderComponent.buildListPageConfigHtml("101");
    }
    private String buildPurchaserListHtml(){
        return htmlListPageBuilderComponent.buildListPageConfigHtml("102");
    }
    private String buildPracticeListHtml(){
        return htmlListPageBuilderComponent.buildListPageConfigHtml("103");
    }
    private String buildSupervisorListHtml(){
        return htmlListPageBuilderComponent.buildListPageConfigHtml("104");
    }
    private String buildInstituteListHtml(){
        return htmlListPageBuilderComponent.buildListPageConfigHtml("105");
    }
    private String buildArmyListHtml(){
        return htmlListPageBuilderComponent.buildListPageConfigHtml("106");
    }

    private String buildAutomobileListHtml(){
        return htmlListPageBuilderComponent.buildListPageConfigHtml("107");
    }
    private String buildItListHtml(){
        return htmlListPageBuilderComponent.buildListPageConfigHtml("108");
    }
    private String buildElevatorListHtml(){
        return htmlListPageBuilderComponent.buildListPageConfigHtml("109");
    }
    private String buildConditionerListHtml(){
        return htmlListPageBuilderComponent.buildListPageConfigHtml("110");
    }
    private String buildFurnitureListHtml(){
        return htmlListPageBuilderComponent.buildListPageConfigHtml("111");
    }
    private String buildHubeiListHtml(){
        return htmlListPageBuilderComponent.buildListPageConfigHtml("1401");
    }
}
